import 'package:flutter/material.dart';

class TextFieldDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => TextFieldState();
}

class TextFieldState extends State<TextFieldDemo> {
  @override
  void initState() {
    super.initState();
    textEditingContrallor.text = 'input you phone';

    textEditingContrallor.addListener(() {
      debugPrint("input =${textEditingContrallor.text}");
    });
  }

  final textEditingContrallor = TextEditingController();

  @override
  void dispose() {
    textEditingContrallor.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
//        body:
//        )

      body: Theme(
          data: Theme.of(context).copyWith(primaryColor: Colors.green),
          child: Container(
            margin: EdgeInsets.all(10.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
//                TextField(
////                  onChanged: (value) {
////                    debugPrint('phone number onChange =$value');
////                  },
//
//                  controller: textEditingContrallor,
//                  onSubmitted: (value) {
//                    debugPrint('phone number onSubmitted =$value');
//                  },
//                  maxLines: 1,
//                  decoration: InputDecoration(
//                      hintText: 'input you phone', labelText: 'title'),
//                )

                RegistForm()
              ],
            ),
          )),
    );
  }
}

class RegistForm extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RegistState();
}

class RegistState extends State<RegistForm> {
  var glableKey = GlobalKey<FormState>();
  bool _autovalidate = false;
  String _userName;
  String _passWord;

  @override
  void initState() {
    super.initState();
  }

  void _onRegistSaveValue() {
    bool success = glableKey.currentState.validate();
    if (success) {
      glableKey.currentState.save();
      debugPrint(
          '_onRegistSaveValue UserName = $_userName =PassWord= $_passWord');

      Scaffold.of(context).showSnackBar(SnackBar(content: Text('Registing......')));
    } else {
      setState(() {
        _autovalidate = true;
      });
    }
  }

  String _validateUserName(String value) {
    if (value.isEmpty) {
      return 'UserName is required';
    }
    return null;
  }

  String _validatePassWord(String value) {
    if (value.isEmpty) {
      return 'PassWord is required';
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Form(
        key: glableKey,
        child: Column(
          children: <Widget>[
            TextFormField(
              decoration: InputDecoration(labelText: 'Username'),
              onSaved: (value) {
                _userName = value;
              },
              validator: _validateUserName,
              autovalidate: _autovalidate,
            ),
            TextFormField(
              obscureText: true,
              decoration: InputDecoration(
                labelText: 'Password',
              ),
              onSaved: (value) {
                _passWord = value;
              },
              validator: _validatePassWord,
              autovalidate: _autovalidate,
            ),
            SizedBox(
              height: 32.0,
            ),
            Container(
              width: double.infinity,
              child: RaisedButton(
                color: Theme.of(context).accentColor,
                child: Text(
                  'Regist',
                  style: TextStyle(color: Colors.white),
                ),
                elevation: 0.0,
                onPressed: _onRegistSaveValue,
              ),
            )
          ],
        ));
  }
}
